import {Cesium} from "@/public/global/const.js";

export default class {
    constructor({viewer}) {
        this.events = {};
        const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        handler.setInputAction((click) => {
            const pick = viewer.scene.pick(click.position);
            if (pick && pick.primitive) {
                const map = this.events[Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK];
                if (map && map.has(pick.primitive)) {
                    map.get(pick.primitive)();
                    return;
                }
                if (map && map.has(pick.id)) {
                    map.get(pick.id)();
                }
            }
        }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
        handler.setInputAction((click) => {
            const pick = viewer.scene.pick(click.position);
            if (pick && pick.primitive) {
                const map = this.events[Cesium.ScreenSpaceEventType.LEFT_CLICK];
                if (map && map.has(pick.primitive)) {
                    map.get(pick.primitive)();
                    return;
                }
                if (map && map.has(pick.id)) {
                    map.get(pick.id)();
                }
            }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    }

    addListen(eventName, primitive, callback) {
        if (!this.events[eventName]) {
            this.events[eventName] = new Map();
        }
        this.events[eventName].set(primitive, callback);
    }

    removeListen(eventName, primitive) {
        this.events[eventName] && this.events[eventName].delete(primitive);
    }
}
